//
//  JourneyInfoViewCotroller.m
//  MyTravel
//
//  Created by Hunk on 11-5-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "JourneyInfoViewCotroller.h"
#import "NetworkManager.h"
#import "MyLoadingView.h"
#import <QuartzCore/QuartzCore.h>

#define TAG_TABLEVIEW 202
#define SELF_TITLE @"参团选择"

@interface JourneyInfoViewCotroller (Private)

-(void)addLabelOnCell:(UIView*)view
                 text:(NSString*)text
                frame:(CGRect)frame 
              bgColor:(UIColor*)bgColor 
            textColor:(UIColor*)textColor 
           fontOfSize:(UIFont*)fontOfSize 
        lineBreakMode:(UILineBreakMode)lineBreakMode 
        textAlignment:(UITextAlignment)textAlignment;

- (void)addJourneyInfo:(UIScrollView*)infoScrollView infoArray:(NSArray*)infoArray;

@end

@implementation JourneyInfoViewCotroller
@synthesize journeyId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    SAFE_RELEASE(arrayTitle)
    SAFE_RELEASE(scrollView)
    SAFE_RELEASE(pageControl)
    SAFE_RELEASE(titleLabel)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self setTitle:SELF_TITLE];
    
    // Init title
    arrayTitle = [[NSArray alloc] initWithObjects:
                  @"团队编号",
                  @"出发地点",
                  @"出发日期",
                  @"航班信息",
                  @"旅行报价",
                  @"剩余名额",
                  @"报名截止", nil];
    
    arrayJourneyInfo = [[NSMutableArray alloc] init];
    
    // Init scrollView
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    scrollView.pagingEnabled = YES;
    scrollView.userInteractionEnabled = YES;
    scrollView.scrollEnabled = YES;
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    
    // Page control
    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 16)];
    pageControl.backgroundColor = [UIColor clearColor];
    pageControl.defersCurrentPageDisplay = YES;
    pageControl.hidesForSinglePage = NO;
    [self.view addSubview:pageControl];
    
    // Title label
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 6, 300, 40)];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setTextColor:[UIColor colorWithRed:76.0/255.0 green:86.0/255.0 blue:108.0/255.0 alpha:1.0]];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];
    [titleLabel setTextAlignment:UITextAlignmentCenter];
    [titleLabel setText:@""];
    [self.view addSubview:titleLabel];
    
    // Data
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                journeyId, @"journeyId", nil];
    
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetJourneyInfo
                                                   WithParameters:parameters
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
    
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
}

#pragma mark -
#pragma mark NetworkManager callback Methods
- (void)onSuccess:(id)sender
{
    NSDictionary* resultDic = (NSDictionary*)sender;
    
    [arrayJourneyInfo removeAllObjects];
    [arrayJourneyInfo addObjectsFromArray:[resultDic objectForKey:@"journeyInfo"]];
    
    [scrollView setContentSize:CGSizeMake(320 * [arrayJourneyInfo count], 416)];
    pageControl.numberOfPages = [arrayJourneyInfo count];
    
    [titleLabel setText:[NSString stringWithFormat:
                         @"第%d/%d种选择",
                         (pageControl.currentPage + 1), [arrayJourneyInfo count]]];
    
    [self addJourneyInfo:scrollView infoArray:arrayJourneyInfo];
}

- (void)onFailed:(id)sender
{
	NSDictionary* resultDic = (NSDictionary*)sender;
    
    // 暂无数据提示
	UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:SORRY_NODATA 
                                                        message:[resultDic objectForKey:@"message"]
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

#pragma mark -
#pragma mark addSights:sight:
- (void)addJourneyInfo:(UIScrollView*)infoScrollView infoArray:(NSArray*)infoArray;
{
    for(int j = 0; j < [infoArray count]; j++)
    {   
        UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(j * 320, 0, 320, 416)];
        [bgView setBackgroundColor:[UIColor clearColor]];
        
        // Temp view
        UIView* tempView = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 300, 350)];
        tempView.backgroundColor = [UIColor whiteColor];
        tempView.layer.cornerRadius = 10.0;
        tempView.layer.masksToBounds = YES;
        tempView.layer.borderWidth = 1.0;
        tempView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        // Line
        for(int i = 0; i < 6; i++)
        {
            UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 50 * (i + 1), 300, 1)];
            [lineView setBackgroundColor:[UIColor lightGrayColor]];
            [tempView addSubview:lineView];
            [lineView release];
        }
        
        for(int i = 0; i < 7; i++)
        {
            // Menu title
            [self addLabelOnCell:tempView
                            text:[[arrayTitle objectAtIndex:i] stringByAppendingString:@":"] 
                           frame:CGRectMake(10, 5 + i * 50, 80, 40)
                         bgColor:[UIColor clearColor]
                       textColor:[UIColor blackColor]
                      fontOfSize:[UIFont boldSystemFontOfSize:18]
                   lineBreakMode:UILineBreakModeCharacterWrap
                   textAlignment:UITextAlignmentLeft];
            
            // Text
            NSString* textString;
            NSDictionary* tempDic = [arrayJourneyInfo objectAtIndex:j];
            
            switch (i)
            {
                case 0:
                    textString = [tempDic objectForKey:@"team_id"];
                    break;
                case 1:
                    textString = [tempDic objectForKey:@"starting_address"];
                    break;
                case 2:
                    textString = [tempDic objectForKey:@"starting_time"];
                    break;
                case 3:
                    textString = [tempDic objectForKey:@"flight_info"];
                    break;
                case 4:
                    textString = [NSString stringWithFormat:@"¥%@", [tempDic objectForKey:@"journey_price"]];
                    break;
                case 5:
                    textString = [tempDic objectForKey:@"last_seat"];
                    break;
                case 6:
                    textString = [tempDic objectForKey:@"end_time"];
                    break;
                default:
                    break;
            }
            
            [self addLabelOnCell:tempView
                            text:textString
                           frame:CGRectMake(80, 5 + i * 50, 210, 40)
                         bgColor:[UIColor clearColor]
                       textColor:TEXTCOLOR
                      fontOfSize:[UIFont fontWithName:@"AmericanTypewriter" size:18]
                   lineBreakMode:UILineBreakModeCharacterWrap
                   textAlignment:UITextAlignmentRight];
        }
        
        [bgView addSubview:tempView];
        [tempView release];
        [infoScrollView addSubview:bgView];
        [bgView release];
        
        /*
        // Bg view
        UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, 416)];
        bgView.backgroundColor = [UIColor clearColor];
        
        // Add tableView
        UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)
                                                              style:UITableViewStyleGrouped];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        [tableView setTag:TAG_TABLEVIEW + i];
        [bgView addSubview:tableView];
        [tableView release];
        
        [infoScrollView addSubview:bgView];
        [bgView release];
        */
    }
}

#pragma mark -
#pragma mark scrollViewDidEndDecelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)infoScrollView
{   
    pageControl.currentPage = infoScrollView.contentOffset.x / 320;
    
    [titleLabel setText:[NSString stringWithFormat:
                         @"第%d/%d种选择",
                        (pageControl.currentPage + 1), [arrayJourneyInfo count]]];
}

#pragma mark alertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark addLabelOnCell
-(void)addLabelOnCell:(UIView*)view
				 text:(NSString*)text
				frame:(CGRect)frame 
			  bgColor:(UIColor*)bgColor 
			textColor:(UIColor*)textColor 
		   fontOfSize:(UIFont*)fontOfSize 
		lineBreakMode:(UILineBreakMode)lineBreakMode 
		textAlignment:(UITextAlignment)textAlignment
{
	if(!bgColor) bgColor = [UIColor grayColor];
	if(!textColor) textColor = [UIColor blackColor];
	if(!fontOfSize) fontOfSize = [UIFont systemFontOfSize:16];
	if(!text) text = [NSString stringWithString:@"请输入Label内容"];
	
	UILabel* textLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
	textLabel.backgroundColor = bgColor;
	textLabel.textColor = textColor;
	textLabel.font = fontOfSize;
	textLabel.lineBreakMode = UILineBreakModeCharacterWrap;
	textLabel.textAlignment = textAlignment;
	textLabel.text = text;
	[view addSubview:textLabel];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
